const readline = require('readline')

module.exports = {
  /**
   * 通过命令行用户提出问题, 并获取用户输入的答案
   * @param {string} query
   */
  question(query, multiLine = false) {
    return new Promise((resolve) => {
      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      })
      if (multiLine) {
        const lines = []
        console.log(query)
        rl.on('line', (line) => {
          if (line) {
            lines.push(line)
          } else {
            resolve(lines.join('\n'))
            rl.close()
          }
        })
      } else {
        rl.question(query, (answer) => {
          resolve(answer)
          rl.close()
        })
      }
    })
  }
}